#!/bin/sh

PATH=/opt/bin:/opt/sbin:/opt/libexec:/bin:/usr/sbin:/usr/bin:/usr/local/bin:
export PATH

. /opt/etc/slimserver.conf

#
#  Check if user/group slimserver exists
#
        if test `grep -c "^${SLIMSERVER_USER}" /etc/passwd` -eq 0 ; then 
            test `grep -c "^${SLIMSERVER_GROUP}" /etc/group` -eq 0  &&  addgroup ${SLIMSERVER_GROUP}
            echo -n "Enter SLIMSERVER_HOME location [${SLIMSERVER_HOME}]: "
            read answer
            test  "$answer" != "" && SLIMSERVER_HOME=$answer
            if test ! -d SLIMSERVER_HOME ; then
	        adduser -D -s /dev/null -h ${SLIMSERVER_HOME} -G ${SLIMSERVER_GROUP} ${SLIMSERVER_USER} 2>/dev/null
	        sleep 1 # wait for user creation
                sed -i -e "s|^SLIMSERVER_HOME=.*|SLIMSERVER_HOME=${SLIMSERVER_HOME}|" \
                    -e "s|^SLIMSERVER_LOG=.*|SLIMSERVER_LOG=${SLIMSERVER_HOME}/slimserver.log|" \
                    -e "s|^SLIMSERVER_PREF=.*|SLIMSERVER_PREF=${SLIMSERVER_HOME}/slimserver.pref|" \
                    -e "s|^SLIMSERVER_PIDFILE=.*|SLIMSERVER_PIDFILE=${SLIMSERVER_HOME}/slimserver.pid|" \
                    -e "s|^SLIMSERVER_CACHEDIR=.*|SLIMSERVER_CACHEDIR=${SLIMSERVER_HOME}/cache|" \
                    /opt/etc/slimserver.conf
                . /opt/etc/slimserver.conf
            fi
	fi
        touch  ${SLIMSERVER_PREF} 
	chown $VERBOSE ${SLIMSERVER_USER}:${SLIMSERVER_GROUP} ${SLIMSERVER_PREF}

	touch  ${SLIMSERVER_LOG}
	chown $VERBOSE ${SLIMSERVER_USER}:${SLIMSERVER_GROUP} ${SLIMSERVER_LOG}

	mkdir ${SLIMSERVER_HOME}/cache 2>/dev/null
	chown $VERBOSE -R ${SLIMSERVER_USER}:${SLIMSERVER_GROUP} ${SLIMSERVER_HOME} 

# Slimserver expects an mysqld in its Bin directory - on fsg3 mysqld is pre-installed.
# on other systems copy or link from another directory.
	if test -e /usr/local/mysql/bin/mysqld ; then
		ln -sf /usr/local/mysql/bin/mysqld /opt/share/slimserver/Bin/mysqld
	fi
	if test -e /opt/libexec/mysqld ; then
		ln -sf /opt/libexec/mysqld /opt/share/slimserver/Bin/mysqld
	fi

        

# The firmware bin files cannot be distributed and will be downloadd by slimserver
# on first startup so user slimserver has to be able to write files into Firmware directory

	chmod a+rw /opt/share/slimserver/Firmware

exit 0
	
